#include <iostream>

using namespace std;

typedef int Qdata;

class Queue {
	private:
		struct Node {
			Qdata data;
			struct Node *next;
			// struct Node * pre, *next;
		};
		int qlen;
		Node * Qhead;
		Node * Qtail;

	public:
		Queue() {
			// Qhead->pre = NULL;
			Qhead = NULL;
			Qtail = NULL;
			qlen = 0;
		}
		~Queue() {
			cout << "I'm diying ..." << endl;
		}
		
		Qdata front();
		bool pop();
		bool push(Qdata &data);
		bool empty();
		int length();
};

Qdata Queue::front() {
	return Qhead->data;
}

bool Queue::pop() {
	Node * temp = Qhead;
	
	Qhead = Qhead->next;
	delete temp;
	qlen--;

	return true;
}

bool Queue::push(Qdata &data) {
	Node * temp = new Node;
	temp->data = data;
	temp->next = NULL;

	if(Qtail != NULL)
		Qtail->next = temp;
	else
		Qhead = temp;

	Qtail = temp;
	qlen++;

	return true;
}

int Queue::length() {
	return qlen;
}

bool Queue::empty() {
	return (qlen == 0);
}

int main(int argc, char *argv[]) {
	// 创建对象
	Queue q1, q2, q3;

	if( ! q1.empty() )
		cout << "Q1 is empty." << endl;
	else
		cout << "Q1 is not empty." << endl;

	for(int i=2; i< 26; i++) {
		q1.push(i);
	}
	
	cout << "Before pop, Q1 top element is: " << q1.front() << endl; 
	q1.pop();
	cout << "After pop, Q1 top element is: " << q1.front() << endl; 
	cout << "Now Q1 length is: " << q1.length() << endl;

	cout << "Before pop, Q1 top element is: " << q1.front() << endl; 
	q1.pop();
	cout << "After pop, Q1 top element is: " << q1.front() << endl; 
	cout << "Now Q1 length is: " << q1.length() << endl;

	int in;
	cout << "Please Input Data to push: ";
	cin >> in;
	q1.push(in);
	cout << "Top element is : " << q1.front() << endl;
	cout << "Now Q1 length is: " << q1.length() << endl;

	int j = 0;
	cout << "All the data in the queue: " << endl;
	while(!q1.empty()) {
		cout << j++ <<" element is: " << q1.front() << endl; 
		q1.pop();
	}

	return 0;
}
